package org.example;

import org.example.utils.DBUilt;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo03_Insert {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement statement = null;


        try {
            connection = DBUilt.getConnection();
            String sql = "insert into student (sn,name,mail,class_id) values (?,?,?,?)";
            statement = connection.prepareStatement(sql);
            System.out.println("请输入学号:");
            Scanner scanner = new Scanner(System.in);
            String sn = scanner.next();

            System.out.println("请输入姓名:");
            String name = scanner.next();

            System.out.println("请输入邮箱:");
            String mail = scanner.next();

            System.out.println("请输入班级编号:");
            Long classId = Long.valueOf(scanner.next());

            // 5. 用真实数据填充占位符
            statement.setString(1, sn);
            statement.setString(2, name);
            statement.setString(3, mail);
            statement.setLong(4, classId);

            int row  = statement.executeUpdate();

            if(row==1) System.out.println("成功插入！");
            else System.out.println("插入失败！");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUilt.close(null,statement,connection);
        }
    }
}

















